package entities.concept;

import java.util.Iterator;
import java.util.Set;

import option.KBVocabulary;

import utils.Display;


public final class OrConcept extends NaryConcept {

	public OrConcept(Set<Concept> subs) {
		super(subs);
	}

	public OrConcept(Concept c1, Concept c2) {
		this.cons.add(c1);
		this.cons.add(c2);
	}

	private volatile int hashCode = 0;

	@Override
	public int hashCode() {
		if (hashCode != 0) {
			return hashCode;
		}
		final int prime = 31;
		int result = 17;
		for (Concept c : cons) {
			result = prime * result + ((c == null) ? 0 : c.hashCode());
		}
		hashCode = result;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!(obj instanceof OrConcept))
			return false;
		OrConcept that = (OrConcept) obj;
		if (cons == null) {
			if (that.cons != null)
				return false;
		} else if (!cons.equals(that.cons))
			return false;
		return true;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("[");
		Iterator<Concept> iter = cons.iterator();
		while (iter.hasNext()) {
			sb.append(iter.next().toString());
			if (iter.hasNext()) {
				sb.append(" " + Display.ORUNICODE + " ");
			}
		}
		sb.append("]");
		return sb.toString();
	}

	public String toSerializedString() {
		StringBuilder sb = new StringBuilder();
		sb.append(" (" + KBVocabulary.OR + " ");
		for (Concept c : cons) {
			sb.append(c.toSerializedString() + " ");
		}
		sb.append(") ");
		return sb.toString();
	}

}
